import os

# 统计指定目录的大小
def count_dir(dir):
    dirlist = os.listdir(dir)
    # print(dirlist)
    sum = 0
    for f in dirlist:
        file1 = os.path.join(dir, f)
        if os.path.isfile(file1):
            fsize = os.path.getsize(file1)
            # print(file1, '=', fsize)
            sum = sum + fsize
            # print('sum = ', sum)
        if os.path.isdir(file1):
            # print(file1, 'is a dir!')
            sum = sum + count_dir(file1)
    return sum


# 以下是测试代码
# sum = count_dir('/Users/wangming/Desktop/python')
sum = count_dir('/Users/wangming/Desktop/温州之旅2019')
print('sum is', sum)
